/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.user.ctrl;

import com.arg.deploy.bean.User;
import com.arg.deploy.common.security.SecurityMD5;
import com.arg.deploy.core.hibernate.UserCore;
import com.arg.deploy.webapp.common.AlertMessages;
import com.arg.deploy.webapp.common.MainPageController;
import com.arg.deploy.webapp.common.ResourceUtil;
import com.arg.deploy.webapp.common.ValidateManager;
import com.arg.deploy.webapp.common.ValidateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Textbox;

/**
 *
 * @author sorasaks
 */
public class AddUserCtrl extends MainPageController {

    private static final Logger LOG = Logger.getLogger(AddUserCtrl.class);
    private Textbox txtSearch;
    private Button btnSearch;
    private Button btnAdd;
    private Textbox txtUsername;
    private Textbox txtPassword;
    private Textbox txtRePassword;
    private Grid gridUser;
    private Label lbTotal;
    private AddUserRenderer renderer;

    public AddUserCtrl() {
        super("DEPLOY ::: Add User", Boolean.FALSE);
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        initialEvenListener();
        initialComponent();
    }

    private void initialComponent() {
        doSearch(null);
    }

    private void initialEvenListener() {
        btnAdd.addEventListener(Events.ON_CLICK, new AddNewUser());
        btnSearch.addEventListener(Events.ON_CLICK, new SearchUser());
    }

    private class AddNewUser implements EventListener {

        @Override
        public void onEvent(Event t) throws Exception {
            if (!validateAdd()) {
                return;
            }
            User user = new User();
            user.setUsername(txtUsername.getValue());
            SecurityMD5 md5 = new SecurityMD5(txtPassword.getValue());
            user.setPassword(md5.encoding());
            user.setCreateDate(new Date());

            Boolean isSave = UserCore.save(user);

            if (!isSave) {
                AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.failed.save", locale));
                return;
            }

            AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.save", locale));
            doSearch(null);
        }
    }

    private class SearchUser implements EventListener {

        @Override
        public void onEvent(Event t) throws Exception {
            List<User> list = new ArrayList<User>();
            if (StringUtils.isEmpty(txtSearch.getValue())) {
                list = UserCore.getUser();
            } else {
                User user = UserCore.getUserByUsername(txtSearch.getValue());
                if (user == null) {
                    AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.no.data", locale));
                    return;
                }
                list.add(user);
            }

            doSearch(list);
        }
    }

    private void doSearch(List<User> list) {
        if (list == null) {
            list = UserCore.getUser();
        }
        renderer = new AddUserRenderer(locale);
        lbTotal.setValue(String.valueOf(list.size()));
        gridUser.setRowRenderer(renderer);
        gridUser.setModel(new ListModelList<User>(list));
    }

    private Boolean validateSearch() {
        ValidateManager vm = new ValidateManager();
        vm.add(ValidateUtil.requireField(txtSearch));
        if (!vm.getValue()) {
            AlertMessages.alertMessage(vm.getMessageTemplate());
        }
        return vm.getValue();
    }

    private Boolean validateAdd() {
        ValidateManager vm = new ValidateManager();
        vm.add(ValidateUtil.requireField(txtUsername));
        vm.add(ValidateUtil.requireField(txtPassword));
        vm.add(ValidateUtil.requireField(txtRePassword));

        if (!vm.getValue()) {
            AlertMessages.alertMessage(vm.getMessageTemplate());
            return Boolean.FALSE;
        }

        if (txtPassword.getValue().length() < 7) {
            vm.add(ValidateUtil.addError(txtPassword, "ผิดพลาด ::: รหัสผ่านอย่างน้อยต้อง 6 ตัวอักษรขึ้นไป"));
            AlertMessages.alertMessage(vm.getMessageTemplate());
            return Boolean.FALSE;
        }

        if (!txtPassword.getValue().equals(txtRePassword.getValue())) {
            vm.add(ValidateUtil.addError(txtPassword, "ผิดพลาด ::: รหัสผ่านใหม่กรอกไม่ตรงกัน"));
            vm.add(ValidateUtil.addError(txtRePassword, "ผิดพลาด ::: รหัสผ่านใหม่กรอกไม่ตรงกัน"));
            AlertMessages.alertMessage(vm.getMessageTemplate());
            return Boolean.FALSE;
        }

        return vm.getValue();
    }
}
